
package com.tianyang.yukuaifu.base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.utils.permissions.PermissionsMgr;
import com.tianyang.yukuaifu.utils.permissions.PermissionsResultAction;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/**
 * Description : 主要是为了需要权限的可继承此类
 */
public abstract class PermissionActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.ThemeLeftAndRightAnimation);
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            fixOrientation();
        }
        super.onCreate(savedInstanceState);
    }

    private final String TAG = this.getClass().getSimpleName();

    /**
     * 当权限被授权
     */
    public void onPermissionGranted(List<String> permissions) {
    }

    /**
     * 当权限被拒绝
     *
     * @param permissions 申请的权限
     */
    public void onPermissionDenied(List<String> permissions) {
    }

    /**
     * 是否有某项权限
     */
    public boolean hasPermission(String... permission) {
        return PermissionsMgr.getInstance().hasAllPermissions(this, permission);
    }

    /**
     * 校验是否有某项权限，没有返回是哪种权限
     */
    public String checkPermission(String... permission) {
        return PermissionsMgr.getInstance().checkPermissions(this, permission);
    }

    /**
     * 申请权限
     *
     * @param permissions 需要的权限
     */
    public void requestPermissions(String... permissions) {
        if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)) {
            PermissionsMgr.getInstance().requestPermissionsIfNecessaryForResult(this, permissions,
                    new PermissionsResultAction() {

                        @Override
                        public void onGranted(List<String> perms) {
                            Log.d(TAG, "Permission is Granted:" + perms);
                            onPermissionGranted(perms);
                        }

                        @Override
                        public void onDenied(List<String> perms) {
                            Log.d(TAG, "Permission is Denied" + perms);
                            onPermissionDenied(perms);
                        }
                    });
        } else {
            onPermissionGranted(Arrays.asList(permissions));
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
            @NonNull int[] grantResults) {
        PermissionsMgr.getInstance().notifyPermissionsChange(permissions, grantResults);
    }

    @Override
    public void setRequestedOrientation(int requestedOrientation) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            return;
        }
        super.setRequestedOrientation(requestedOrientation);
    }

    private boolean isTranslucentOrFloating(){
        boolean isTranslucentOrFloating = false;
        try {
            int [] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray ta = obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (boolean)m.invoke(null, ta);
            m.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }

    /**
     * Android 8.0 上出现 Only fullscreen opaque activities can request orientation 的解决情况
     * @return
     */
    private boolean fixOrientation(){
        try {
            Field field = Activity.class.getDeclaredField("mActivityInfo");
            field.setAccessible(true);
            ActivityInfo o = (ActivityInfo)field.get(this);
            o.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
            field.setAccessible(false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
